package com.topvision.elementanim;

import android.animation.AnimatorSet;
import android.animation.ArgbEvaluator;
import android.animation.IntEvaluator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import android.view.WindowManager;

/**
 * Author: lucy
 * Comment: //TODO
 * Date: 2017-09-27 17:01
 */

public class MyView extends View {

    private int background;
    private int rectLength;
    private int[] bg;
    private ObjectAnimator colorAnimator;
    private ObjectAnimator rectAnimator;
    private int mWidth;
    private int mHeight;
    private int viewWidth;
    private int viewHeight;
    private Paint mPaint;

    public MyView(Context context) {
        this(context,null);
    }

    public MyView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        mWidth = windowManager.getDefaultDisplay().getWidth();
        mHeight = windowManager.getDefaultDisplay().getHeight();
        bg = new int[]{ContextCompat.getColor(getContext(), R.color.color_red),
                ContextCompat.getColor(getContext(), R.color.color_orange),
                ContextCompat.getColor(getContext(), R.color.color_yellow),
                ContextCompat.getColor(getContext(), R.color.color_green),
                ContextCompat.getColor(getContext(), R.color.color_blue)};
        mPaint = new Paint();
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAntiAlias(true);
        background = bg[0];
        colorAnimator = ObjectAnimator.ofInt(this, "background", bg[0], bg[0]);
        colorAnimator.setIntValues(bg[0],bg[1],bg[2],bg[3],bg[4]);
        colorAnimator.setEvaluator(new ArgbEvaluator());
        colorAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                background = (int) valueAnimator.getAnimatedValue();
                invalidate();
            }
        });

        rectAnimator = ObjectAnimator.ofInt(this, "rect", 0, 0);
        rectAnimator.setIntValues(0, 1200);
        rectAnimator.setEvaluator(new IntEvaluator());
        rectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                rectLength = (int) valueAnimator.getAnimatedValue();
                invalidate();
            }
        });
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        viewWidth = getMeasuredWidth();
        viewHeight = getMeasuredHeight();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(background);
//        canvas.drawRect(0, viewHeight / 2, rectLength, viewHeight / 2,mPaint);
        canvas.drawRect(0, mHeight / 2, rectLength, mHeight / 2 + 20, mPaint);
    }

    public void startAnim() {
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(rectAnimator,colorAnimator);
        animatorSet.setDuration(5000);
        animatorSet.start();
    }
}
